package com.guangaiboen.attendance.wx.web;

import com.guangaiboen.attendance.core.dispatcher.utils.ObjectUtils;
import com.guangaiboen.attendance.core.util.RegexUtil;
import com.guangaiboen.attendance.core.util.ResponseUtil;
import com.guangaiboen.attendance.core.validator.Order;
import com.guangaiboen.attendance.core.validator.Sort;
import com.guangaiboen.attendance.db.domain.AttendanceAd;
import com.guangaiboen.attendance.db.domain.resp.AttendanceInfoResp;
import com.guangaiboen.attendance.db.service.AttendanceInfoService;
import com.guangaiboen.attendance.wx.annotation.LoginUser;
import com.guangaiboen.attendance.wx.dto.DutyRequest;
import com.guangaiboen.attendance.wx.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.guangaiboen.attendance.wx.util.WxResponseCode.*;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户值班信息
 */
@RestController
@RequestMapping("/wx/duty")
@Validated
public class WxAttendanceInfoController {

    @Autowired
    private AttendanceInfoService attendanceInfoService;

    /**
     * 值班信息提交
     * @param dutyRequest
     * @return
     */
    @PostMapping("submitduty")
    public Object submitDuty(@RequestBody DutyRequest dutyRequest) {
        Integer userId = dutyRequest.getUserId();
        LocalDateTime startTime = dutyRequest.getStartTime();
        LocalDateTime endTime = dutyRequest.getEndTime();
        //1.非空校验
        if(ObjectUtils.isEmpty(userId)){
            return ResponseUtil.unlogin();
        }
        if(ObjectUtils.isEmpty(startTime)){
            return ResponseUtil.fail(EMPTY_DUTY_STARTDATE, "值班开始时间不能为空");
        }
        if(ObjectUtils.isEmpty(endTime)){
            return ResponseUtil.fail(EMPTY_DUTY_ENDDATE, "值班结束时间不能为空");
        }
        Date startDate = Date.from( startTime.atZone( ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from( endTime.atZone( ZoneId.systemDefault()).toInstant());
        //2.日期校验,开始时间必须大于系统当前时间，
//       if(DateUtils.compareTwoDates(startDate,new Date())){
//           return ResponseUtil.fail(DUTY_STARTDATE_LT_NOW, "值班开始时间不能小于系统当前时间");
//       }
        if(DateUtils.compareTwoDates(endDate,startDate)){
            return ResponseUtil.fail(DUTY_STARTDATE_GT_DUTY_ENDDATE, "值班结束时间不能大于值班开始时间");
        }
        //值班打卡信息提交
        attendanceInfoService.submitDuty(userId,startTime,endTime);
        return ResponseUtil.ok("值班信息填写成功");
    }


    /**
     * 值班列表查询
     * @param userId
     * @param userName 姓名
     * @param deptName 部门名称
     * @param startTime 值班开始时间
     * @param endTime 值班结束时间
     * @param page 当前页数
     * @param limit 每页展示条数
     * @return
     */
    @GetMapping("dutyList")
    public Object list(@LoginUser Integer userId,
                       @RequestParam String userName,
                       @RequestParam String deptName,
                       @RequestParam String startTime,
                       @RequestParam String endTime,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {
        //当前用户没登录，提示未登录，不允许查询值班列表信息
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        List<AttendanceInfoResp> dutyList = attendanceInfoService.dutyList(userName, deptName, startTime, endTime, page, limit);
        return ResponseUtil.okList(dutyList);
    }




}
